package com.lp.mgt.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lp.mgt.commons.CustomResult;
import com.lp.mgt.entities.Role;

import com.lp.mgt.services.RoleService;

@RestController
@RequestMapping("/role")
public class RoleCtr {
	@Autowired
	private RoleService roleService;
	

	@GetMapping("/listPage")
	public ModelAndView toUserPage(ModelAndView mv) {
		mv.setViewName("role/roleList.html");
		return mv;
	}
	
	@GetMapping("/list")
	@RequiresPermissions("role:list")
	public CustomResult getAllUsers(Role role){
		Map<String,Object> map=new HashMap<>();
		map=roleService.getUsersWithTerms(role);
		return new CustomResult().success(true, map);
		//return map;
	}	
	
	@PostMapping("/updateRole")
	public CustomResult<Role> updateRoleWhenEdit(@RequestBody Role role){
		Boolean flag=roleService.updateRoleByIdWhenEdit(role);
		return new CustomResult().success(flag);
	}
	//批量保存
	@PostMapping("/saveRoles")
	public CustomResult<Role> saveRolesByBatch(@RequestBody List<Role> roles){
		Boolean flag=roleService.saveRolesByBatch(roles);
		return new CustomResult().success(flag);
	}
	
	
	@GetMapping("/removeRole")
	public CustomResult<Role> deleteRoleById(String rid){
		Boolean flag=roleService.deleteRoleById(rid);
		return new CustomResult().success(flag);
	}
	
}
